﻿@page "/Admin/Account"
@attribute [Authorize]
@attribute [TabItemOption(Text ="基本设置",Icon ="fa fa-th")]
@inherits BasePage
@implements IDisposable
@inject ISysUserService SysUserService
@inject ToastService Toast

<Tab Placement="Placement.Left" class="w-fit t-none">
    <TabItem Text="基本设置">
        <ValidateForm Model="@Model" OnInvalidSubmit="@OnInvalidSubmit" OnValidSubmit="@OnValidSubmit">

            <div class="container-fluid">
                <div class="row">
                    <div class="col-6">
                        <div class="row">
                            <div class="col-xxl-6 col-12">
                                <Display @bind-Value="@Model.Account" />
                            </div>

                            <div class="col-xxl-6 col-12">
                                <Display @bind-Value="@Model.Name" />
                            </div>

                            <div class="col-xxl-6 col-12">
                                <BootstrapInput @bind-Value="@Model.NickName" />
                            </div>

                            <div class="col-xxl-6 col-12">
                                <DateTimePicker @bind-Value="@Model.BirthdayEx" />
                            </div>

                            <div class="col-xxl-6 col-12">
                                <RadioList class="w-0" @bind-Value="@Model.Sex"  />
                            </div>

                            <div class="col-xxl-6 col-12">
                                <BootstrapInput @bind-Value="@Model.Phone" />
                            </div>
                        
                            <div class="col-xxl-6 col-12">
                                <BootstrapInput @bind-Value="@Model.Tel" />
                            </div>

                            <div class="col-xxl-6 col-12">
                                <BootstrapInput @bind-Value="@Model.Email" />
                            </div>
                        </div>
                    </div>

                    <div class="col-6">
                        <CardUpload TValue="string" @bind-Value="Model.Avatar" IsSingle="true" OnChange="@OnAvatarUpload"/>
                    </div>

                    <div class="col-6">
                        <Button ButtonType="@ButtonType.Submit" Text="更新基本信息" IsAsync="true"/>
                    </div>
                </div>
            </div>

        </ValidateForm>
    </TabItem>

    <TabItem Text="安全设置">
        <div>我是角色管理</div>
    </TabItem>

    <TabItem Text="消息通知">
        <div>我是部门管理</div>
    </TabItem>
</Tab>

@code{
    /// <summary>
    /// 当前会话状态
    /// </summary>
    [CascadingParameter]
    public SessionState State { get; set; } = new();

    private UserOutput Model = new();

    private CancellationTokenSource? ReadAvatarToken { get; set; }

    private long MaxFileLength = 2 * 1024 * 1024;

    protected override void OnInitialized()
    {
        base.OnInitialized();
        Model.Assign(State.Auths);
    }

    private async Task OnAvatarUpload(UploadFile file)
    {
        if (file?.File == null)
        {
            await Toast.Information("上传头像", "未检测到上传图片。");
            return;
        }

        if (file.Size > MaxFileLength)
        {
            await Toast.Information("上传头像", "图片大小必须小于2MB。");
            file.Error = "图片大小超过2MB。";
            file.Code = 1;
            return;
        }

        var format = file.File.ContentType;
        if (!"jpg;png;bmp;gif;jpeg".Split(';').Any(x => format.Contains(x, StringComparison.OrdinalIgnoreCase)))
        {
            await Toast.Information("上传头像", "图片大小必须小于2MB。");
            file.Error = "图片大小超过2MB。";
            file.Code = 1;
            return;
        }

        ReadAvatarToken ??= new CancellationTokenSource();
        if (ReadAvatarToken.IsCancellationRequested)
        {
            ReadAvatarToken.Dispose();
            ReadAvatarToken = new CancellationTokenSource();
        }

        await file.RequestBase64ImageFileAsync(format, 320, 240, MaxFileLength, ReadAvatarToken.Token);
        Model.Avatar = file.PrevUrl;
    }

    private async Task OnInvalidSubmit(EditContext context)
    {
        await Task.CompletedTask;
    }

    private async Task OnValidSubmit(EditContext context)
    {
        if (await SysUserService.EditAsync(Model))
        {
            State.Auths.Assign(Model);
        }
    }

    /// <summary>
    /// Dispose
    /// </summary>
    public void Dispose()
    {
        ReadAvatarToken?.Cancel();
        GC.SuppressFinalize(this);
    }
}